<?php

/**
 * The CPanelCore class is a controller of the Design Pattern.
 * 
 * @abstract LayoutPage
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */
class CPanelCore extends LayoutPage {

    /** @var stdClass The data of the account */
    public static $account;

    /** @var string The date of the last accessed */
    public static $customers_vehicles = null;

    /** @var string The date of the last accessed */
    public static $lastaccessed;

    /** @var array The data of the proposals received */
    public static $proposals = null;

    /** @var array The data of the proposals received */
    public static $proposals_products = null;

    /** @var array The data of the vehicles ads */
    public static $vehicles_ads = null;

    /** @var stdClass The data of the ad of vehicle to be changed */
    public static $vehicle_ad = null;

    /** @var array The data of the products ads */
    public static $products_ads = null;

    /** @var stdClass The data of the ad of products  to be changed */
    public static $products_ad = null;

    /** @var stdClass The data of the ad of products status */
    public static $products_status = null;

    /** @var stdClass The data of the exeded products */
    public static $products_exceded = null;
    
    /** @var stdClass The data of the exeded products categories */
    public static $products_categories = null;

    /** @var stdClass The data of status enterprises */
    public static $enterprises_status = null;

    /** @var bool Defines if the limit of ads registered is exceeded */
    public static $exceeded_ad = false;

    /** @var bool Defines if the limit of ads registered is exceeded */
    public static $exceeded_product = false;

    /** @var bool Defines para avisar o pagamento */
    public static $alertpay = null;

    /** @var int The quantity of news proposals */
    public static $proposals_newsrow;

    /** @var int The quantity of news proposals products */
    public static $product_proposals_newsrow;

    /** */
    public function setMedia() {
        parent::setMedia();

        Shell::addStyle('colorbox');
        Shell::addScript(array('jquery.maskedinput.min', 'jquery.maskmoney', 'jquery.colorbox-min'));
    }

    public function preProcess() {
        parent::preProcess();

        if (!Util::is_logged()) {
            header('Location: ' . __BASE_URI__ . 'login');
            exit();
        }
    }

    public function displayHeader() {
        global $_tag_title, $_metatag_description, $_metatag_keywords;

        $_tag_title = 'Central do Cliente | Goiautos.com.br';
        $_metatag_description = 'Painel de controle do portal Goiautos.';
        $_metatag_keywords = str_word_count(mb_strtolower('carros motos novos seminovos usados caminhões veículos classificados goiás central painel login acesso', 'UTF-8'), 1, 'áéíóúàâêôçãõ0123456789');

        parent::displayHeader();
    }

    public function process() {
        $session = new Session('account');

        self::$proposals_newsrow = DbGet::proposals_newsrow($session->id, $session->type);
        self::$product_proposals_newsrow = DbGet::proposals_products_numrows($session->id, $session->type);
        self::$products_categories = DbGet::products_categories();

        if (!DbGet::products_exeded($session->id, $session->type)) {
            //se nao encontra nenhum registro, a quantidade de produtos foi exedida
            self::$products_exceded = true;
        }

        if ($session->type == 'F')
            self::$account = DbGet::customer($session->id);
        else {
            self::$account = DbGet::enterprise($session->id);
            $numrows = DbGet::enterprise_vehicles_numrows(self::$account->e_id);
            $plan = DbGet::plan(self::$account->id_plan);
            if ($numrows >= $plan->autoquantity)
                self::$exceeded_ad = true;
        }


        if ($accesslog = DbGet::accesslogs($session->id, $session->type, true))
            self::$lastaccessed = date('d/m/Y H:i', strtotime($accesslog->date));
        else
            self::$lastaccessed = '00/00/0000 00:00';

        if (!empty($_GET['param'])) {
            $id = $_GET['param'];
            /*          produto             */
            if ($_GET['page'] == 'alterar-produto') {
                self::$products_ad = DbGet::products_ad($id, $session->type);
                $page = 'products';
            }

            /*          Veículo             */
            if ($session->type == 'F') {
                self::$vehicle_ad = DbGet::customer_vehicle_ad($id);
            } else {
                self::$vehicle_ad = DbGet::enterprise_vehicle_ad($id);
            }
            if (self::$vehicle_ad) {
                if ($_GET['page'] == 'alterar-anuncio') {
                    self::$vehicle_ad->renavam = str_pad(self::$vehicle_ad->renavam, 9, 0, 0);
                    if (strlen(self::$vehicle_ad->cpfcnpj_owner) < 13) {
                        self::$vehicle_ad->cpfcnpj_owner = str_pad(self::$vehicle_ad->cpfcnpj_owner, 11, 0, 0);
                    } else {
                        self::$vehicle_ad->cpfcnpj_owner = str_pad(self::$vehicle_ad->cpfcnpj_owner, 14, 0, 0);
                    }
                    $page = 'createad';
                } elseif ($_GET['page'] == 'remover-anuncio') {
                    $page = 'removead';
                }
            }
        } else {
            if ($_GET['page'] == 'criar-anuncio') {
                if (!self::$exceeded_ad)
                    $page = 'createad';
            } elseif ($_GET['page'] == 'propostas') {
                if ($_GET['param'] == 'veiculos') {
                    self::$proposals = DbGet::proposals($session->id, $session->type);
                } else {
                    self::$proposals_products = DbGet::proposals_products($session->id, $session->type);
                }
                $page = 'proposals';
            } elseif ($_GET['page'] == 'cadastro')
                $page = 'register';
            elseif ($_GET['page'] == 'imagens-da-empresa')
                $page = 'pictures';
            elseif ($_GET['page'] == 'trocar-senha')
                $page = 'chpasswd';
            elseif ($_GET['page'] == 'plano-produto')
                $page = 'productplan';
            elseif ($_GET['page'] == 'criar-anuncio-produto') {
                $page = 'products';

            } elseif ($_GET['page'] == 'gerenciar-anuncios') {
                $page = 'ads';
                self::$products_ads = DbGet::products_ads($session->id, $session->type);
                if ($session->type == 'F') {
                    self::$vehicles_ads = DbGet::customer_vehicles($session->id);
                } else {
                    self::$vehicles_ads = DbGet::enterprise_vehicles($session->id);
                }
            }
        }
        if ($page == null and $_GET['category'] == null) {
            /* Carregar pagina do finenceiro */
            $page = 'financial';
            self::$products_status = DbGet::products_status($session->id, $session->type);

            if ($session->type == 'J') {
                self::$enterprises_status = DbGet::enterprises_status($session->id);
                self::$alertpay = DbGet::enterprise($session->id);
            } elseif ($session->type == 'F') {
                self::$alertpay = DbGet::customer($session->id);
                self::$customers_vehicles = DbGet::customer_vehicles($session->id);
            }
        }

        if (file_exists(_WIDGET_DIR_ . 'cppage-' . $page . '.php'))
            Shell::addWidget('cppage-' . $page, 'cpanel-page');
    }

    public function displayContent() {
        Shell::addWidget(array('cpanel-info', 'cpanel-menus'), 'sidebar-left');
        Shell::showPage('cpanel');
    }

}